/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client_server;

import gdr.maps.MapPaths;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Timer;
import java.util.Vector;
import java.util.jar.JarFile;

import javax.swing.ImageIcon;


/**
 *
 * @author iamdelac
 */
public class RemotePlayer extends javax.swing.JLabel implements Runnable{

	protected final String STAND="player/stand/";
	protected final String WALK="player/walk/";
	protected final String ATTACK="player/attack/";
	protected final String LOOKING="player/looking/";
    public String nome_player;
    private Vector <javax.swing.ImageIcon[]> sprites;
    protected String stato;
    protected String orientation;
    protected RemotePlayer nemicoFocused;
    protected int  offset_x;
    protected int  offset_y;




/**
 * <pre>
 *@param posizione (left || right)
 *@param nome_fighter
 </pre>

 */
    public RemotePlayer (String nome_fighter) {
       super();
      
       sprites = new Vector();
       this.setDoubleBuffered(true);
       this.nome_player = nome_fighter;
       this.orientation = "n/";
       stato = STAND;
       cambiaAnimazione(STAND);
       final RemotePlayer this_instance = this;
       addMouseListener(new java.awt.event.MouseAdapter() {
           public void mousePressed(java.awt.event.MouseEvent evt)
           {
             System.out.println("richiesta attacco");
             Client.instance.mainForm.getMainPlayer().attacca(this_instance);
           }
         });
       }


 
       
    
public void cambiaAnimazione(String animazione){
        offset_x = 46; // devono puntare ai piedi
        offset_y = 74;
        if (animazione.contains("asino"))
        	{
        	offset_x = 60; 
            offset_y = 33;
        	}
	Vector <javax.swing.ImageIcon[]> sprites_temp = new Vector <javax.swing.ImageIcon[]>();
    Vector <String> nomiFile=new Vector();
   try{
    JarFile jarfile = new JarFile(System.getProperty("java.class.path"));
    Enumeration em = jarfile.entries();
    for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements();) {
      Object elem = em1.nextElement();
      if ((""+elem).contains(stato+orientation) && ((""+elem).contains("jpg")||(""+elem).contains("gif")||(""+elem).contains("png")))
   	   {            	   
   	   String[] path =(""+elem).split("/");
   	   nomiFile.add(path[path.length-1]);
   	   }
    }
  }
  catch(Exception ze){
    ze.printStackTrace();
    System.exit(0);
  }
    
    for (String nomeFile : nomiFile) 
    {
     javax.swing.ImageIcon[] orientations = new javax.swing.ImageIcon[8];
	orientations[0] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"n/"+nomeFile));
	orientations[1] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"ne/"+nomeFile));
	orientations[2] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"e/"+nomeFile));
	orientations[3] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"se/"+nomeFile));
	orientations[4] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"s/"+nomeFile));
	orientations[5] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"sw/"+nomeFile));
	orientations[6] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"w/"+nomeFile));
	orientations[7] = new javax.swing.ImageIcon(getClass().getResource("/"+stato+"nw/"+nomeFile));
	sprites_temp.add(orientations);
      sprites = sprites_temp;
    }
   setSize(sprites_temp.get(0)[0].getIconWidth(), sprites.get(0)[0].getIconHeight());

}




public void setStato(String stato){
	this.stato=stato;
}

public void setOrientation(String or)
	{
	 orientation=or;
	}
        



public  void run (){
    String statoOld = stato;
    ImageIcon[] sprite;
    boolean oneCycle = false;
while (true)
     {      
      for( int h=0; h<sprites.size();h++)
        { sprite = sprites.get(h);
          if (!statoOld.equals(stato))
              {
               statoOld = stato;
               cambiaAnimazione(stato);
               break;
              }
      	  if (orientation.equals("n/")) {this.setIcon(sprite[0]);}
          else{if (orientation.equals("ne/")) {this.setIcon(sprite[1]);}
          else{if (orientation.equals("e/")) {this.setIcon(sprite[2]);}
          else{if (orientation.equals("se/")) {this.setIcon(sprite[3]);}
          else{if (orientation.equals("s/")) {this.setIcon(sprite[4]);}
          else{if (orientation.equals("sw/")) {this.setIcon(sprite[5]);}
          else{if (orientation.equals("w/")) {this.setIcon(sprite[6]);}
          else{if (orientation.equals("nw/")) {this.setIcon(sprite[7]);}}}}}}}}  
          try{Thread.sleep(110);}catch(Exception ex){}
        }
      if (oneCycle)
      {
       oneCycle = false;
       if (stato.equals(ATTACK) || stato.equals(LOOKING))
           {stato=STAND;}
      }
  if (statoOld.equals(ATTACK) || statoOld.equals(LOOKING))
      {
       oneCycle = true;
      }
     
                    }

}

@Override
public void setLocation(Point p){
    p.x = p.x - offset_x;
    p.y = p.y - offset_y;
   super.setLocation(p);
}
 @Override
public void setLocation(int x , int y){
    x = x - offset_x;
    y = y - offset_y;
   super.setLocation(x,y);
}

 @Override
 public Point getLocation(){ 
     return new Point (super.getLocation().x+offset_x,super.getLocation().y+offset_y);
 }

    
       
}
